package com.lbm.baseclass.taglib;

import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.lbm.baseclass.constants.Consts;

@SuppressWarnings("serial")
public class AFTAuthTag extends RequestContextAwareTag {

	private String module;
	private String operate;
	private String value;
	private String type;
	private String style;
	private String cssClass;
	private String id;
	private String iconCls;
	private static final String TYPE_DIVBTN = "divbutton";

	@Override
	protected int doStartTagInternal() throws Exception {
		JspWriter out = pageContext.getOut();
		if (!hasPermission()) return SKIP_BODY;
		if (type == null) type = TYPE_DIVBTN;
		if (value == null) value = "";
		String cls = "easyui-linkbutton";
		String tag = "<a>";
		tag = addAttribute(tag, "class", cls + " " + val(cssClass),0);
		tag = addAttribute(tag, "iconCls", val(iconCls),0);
		tag = addAttribute(tag, "plain", "true",0);
		tag = addAttribute(tag, "id", val(id),0);
		tag = tag + value + "</a>";
		out.write(tag);
		
		return EVAL_PAGE;
	}
	public static String addAttribute(final String body, final String name, String value, final int i)
	{
		if (body == null)
		{
			return null;
		}

		if (name == null)
		{
			return body;
		}

		if (value == null)
		{
			value = "";
		}

		final int end = body.indexOf('>', i);

		if (end == -1)
		{
			return body;
		}

		final StringBuffer result = new StringBuffer(body.length());
		result.append(body.substring(i, end)).append(' ');
		result.append(name).append('=').append('"');
		result.append(value+='"');
		result.append(body.substring(end));

		return result.toString();
	}
	@SuppressWarnings("unchecked")
	private boolean hasPermission() {
		boolean flag = false;
		if (module == null || operate == null) return flag;
		List<Map<String, Object>> roleSet = (List<Map<String, Object>>) pageContext
		.getSession().getAttribute(Consts.USER_PRIVILEGES_DATA);
		if (roleSet==null||roleSet.size()==0) return flag;
		for(Map<String,Object> map : roleSet){
			if (module.equals((String)map.get("MODULECODE")) && operate.equals((String)(map.get("OPERATE_CODE")))) {
				flag = true;
			}
		}
		return flag;
	}
	
	private String val(String val) {
		return val == null ? "" : val;
	}
	
	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public String getValue() {
		return value;
	}
	
	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	
	public String getIconCls() {
		return iconCls;
	}
}
